<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 李邦琦 <1536549251@qq.com>
// +----------------------------------------------------------------------

namespace app\crm\controller;
use think\Loader;

class Department extends CrmController
{
    /**
     * 加载模板departmentList
     */
    public function departmentList()
    {
        // 模板输出
        return $this->fetch('departmentList');
    }


    /**
     * 加载模板departmentList
     */
    public function addDepartment()
    {
        $result = Loader::model('Department')->recursionDepartment();
        $result = Loader::model('Department')->recursionDepartmentArr($result);
        $this->assign('options', $result);
        //return view();
        // 模板输出
        return $this->fetch('addDepartment');
    }


    /**
     * 加载模板departmentList
     */
    public function editDepartment()
    {
        $result = Loader::model('Department')->recursionDepartment();
        $result = Loader::model('Department')->recursionDepartmentArr($result, $_GET['id']);
        $this->assign('options', $result);
        $this->assign('id',$_GET['id']);
        //return view();
        // 模板输出
        return $this->fetch('editDepartment');
    }


    /**
     * 增加部门
     * @return [json] [增加结果]
     */
    public function insertDepartment () 
    {
        $id = $this->generateID();
        //获取数据
        $data = array(
            'department_id' => $id,
            'department_name' => $_REQUEST['department_name'],
            'department_pid' => $_REQUEST['department_pid'],
            'department_leader' => $_REQUEST['department_leader']
        );
        // 增加部门
        $result = Loader::model('Department')->insertDepartment($data);
        // echo $result;exit;
        if ($result){
            echo json_encode(array('success'=>true,'msg'=>'部门添加成功！'));
        } else {
            echo json_encode(array('msg'=>'部门添加失败！'));
        }
    }


    /**
     * 加载部门
     * @return [json] [部门列表JOSN字符串]
     */
    public function departmentData () 
    {
        $result = Loader::model('Department')->departmentData();
        foreach ($result as &$val) {
            $val['remark'] = '';
            if ($val['department_pid']) {
                $val['_children'] = true;
            }
        }
        if ($result){
            echo json_encode($result);
        }
    }


    /**
     * 获取编辑数据
     * @return [json/bool] [返回需要编辑的单条数据json字符串/失败返回false]
     */
    public function editData ()
    {
        $id = $_REQUEST['id'];
        if (empty($id)) {
            return false;
        }
        $where = "department_id=$id";
        $result = Loader::model('Department')->departmentData($where);
        if ($result) {
            echo json_encode($result[0]);
        } else {
            return false;
        }
    }


    /**
     * 编辑部门
     * @return [json] [返回编辑状态]
     */
    public function updataDepartment () 
    {
        //获取数据
        $data = array(
            'department_id' => $_REQUEST['department_id'],
            'department_name' => $_REQUEST['department_name'],
            'department_pid' => $_REQUEST['department_pid'],
            'department_leader' => $_REQUEST['department_leader']
        );

        $result = Loader::model('Department')->updataDepartment($data);
        if ($result){
            echo json_encode(array('success'=>true,'msg'=>'部门修改成功！'));
        } else {
            echo json_encode(array('msg'=>'部门修改失败！'));
        }
    }


    /**
     * 删除部门
     * @return [json] [返回删除状态]
     */
    public function delDepartment () 
    {
        $id = $_REQUEST['department_id'];
        $result = Loader::model('Department')->deleteDepartment($id);
        if ($result){
            echo json_encode(array('success'=>true,'msg'=>'删除成功！'));
        } else {
            echo json_encode(array('msg'=>'删除失败！'));
        }
    }





}